package com.jason.reflect;

import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author: Jason
 * @Date: 16/4/8
 * @Time: 11:52
 */
public class TestCase {


    @Test
    public void test1() {

        double a = 20001.4;
        double dou = 1000.50;
        double dou1 = 1002.80;
        double dou2 = 4974.489795918367;
        double dou3 = 2984.69687755102;
        System.out.println(Integer.parseInt(new DecimalFormat("0").format(a)));
//        System.out.println(dou % 1000 == 0);
//
//        BigDecimal bg = new BigDecimal(1007.5);
//        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//        System.out.println(f1);

        double result_sell_big = new BigDecimal(dou3).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(result_sell_big);
        int result_sell_small = new BigDecimal(dou3).setScale(2, BigDecimal.ROUND_HALF_DOWN).intValue();
//        System.out.println(result_sell_small);

//        System.out.println((int) dou1);
//
//        System.out.println("math----" + Math.ceil(dou3));

    }

    @Test
    public void test2() throws UnsupportedEncodingException {
        Map<String, String> param = new HashMap<>();
        param.put("username", "jason");
        param.put("password", "123456");
        StringBuilder sb = new StringBuilder("https://wltest.wanglibao.com/php/redpacks/list/");

        Iterator<Map.Entry<String, String>> iterator = param
                .entrySet().iterator();
        sb.append("?");
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            String key = entry.getKey();
            sb.append(key + "=");
            String value = entry.getValue();
            sb.append(URLEncoder.encode(value, "utf-8"));
            sb.append("&");
        }
        sb.replace(sb.toString().length() - 1, sb.toString().length(), "");
        System.out.println(sb.toString());

    }

    @Test
    public void test3() {
        String value = "MapsEntity{one=15.0, two=15.5, three=16.0, four=16.5, five=17.0, six=17.0, seven=0.0, eight=0.0, nine=0.0, ten=0.0, eleven=0.0, twelve=0.0}";

        String reValue = value.replace("MapsEntity", "");
        System.out.println(reValue);

    }

    @Test
    public void test4() {
        String str = "86.64566666";
        BigDecimal bd = new BigDecimal(Double.parseDouble(str));
        System.out.println(bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

//        System.out.println("=================");
//        DecimalFormat df = new DecimalFormat("#.00");
//        System.out.println(df.format(Double.parseDouble(str)));
//        System.out.println("=================");
//        System.out.println(String.format("%.2f", Double.parseDouble(str)));
//        System.out.println("=================");
//        NumberFormat nf = NumberFormat.getNumberInstance();
//        nf.setMaximumFractionDigits(2);
//        System.out.println(nf.format(Double.parseDouble(str)));

        String url = "https://wltest.wanglibao.com/ylb//js/yuelibao_transfer.pdf";
        if (url.endsWith(".pdf")) {
            System.out.println("yes");
        } else {
            System.out.println("no");
        }

    }

    @Test
    public void test5() {
        double a = 115;
        double b = 0;
        double c = 300;

        System.out.println(a / (b * c));
        System.out.println(a / b / c);

    }

    @Test
    public void test6() {

        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        double value = 6000.0;
        System.out.println(decimalFormat.format(value));

        System.out.println(Category.NEWUSER.getName());

        System.out.println(Math.pow(4, 3));

    }

    private enum Category {
        NEWUSER("新手标");
        private final String category;

        private Category(String category) {
            this.category = category;
        }

        public String getName() {
            return category;
        }

    }


    /**
     * 保留两位小数 并不四舍五入
     */
    @Test
    public void test7() {
        DecimalFormat formater = new DecimalFormat();

        formater.setMaximumFractionDigits(2);
        formater.setGroupingSize(0);
        formater.setRoundingMode(RoundingMode.FLOOR);

        System.out.println(formater.format(5.505034740731389));

        String value = formater.format(5.505034740731389);
        Double result = Double.parseDouble(value);
        System.out.println(new DecimalFormat("#0.00").format(result));

        DecimalFormat formater2 = new DecimalFormat("#0.##");
        System.out.println(formater2.format(123456.7897456));


    }

    @Test
    public void test8() {

        String sessionid = "PHPSESSID=u9nef6gfd6o7e6s7iaj3crn3t3";
        String lar = "laravel_session";
        String value = "eyJpdiI6IkZWa05MTEQ5K3FRdmp5MzFmXC9rMkJBPT0iLCJ2YWx1ZSI6ImNJNHNydU42NjQ0bXdQYXhmajJmN3p4aUFJNjdoSUgrZDhiOG1HM0haWG5mXC8zSXRieXJxdk5rS3JQRUlmMys2RTRFWXlicjUwUmVDRG1FSDd1R1crQT09IiwibWFjIjoiM2Y0MWMxYTFiMjUwMjJjMmZiYTc1NDI5MWFlZmZiOWEzNDRkOTFkYmY3NjgwMGEyOWQ1NjdiNGNlNDU1ZjFkNyJ9";
        if (sessionid.contains(lar)) {
            System.out.println("包含");
        } else {
            System.out.println(sessionid + "; " + lar + "=" + value);
        }

    }

    /**
     * double类型不以科学计数显示
     */
    @Test
    public void test9() {
        double number = 90000000.00;
        System.out.println(number);
        DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
        String resule = decimalFormat.format(number);
        System.out.println(resule);

        // 另一种方法
        BigDecimal bigDecimal = new BigDecimal("123456789.123456789");
        String result = bigDecimal.toString();
        System.out.println(result);

        DecimalFormat decimalFormat1 = new DecimalFormat("#,##0");
        System.out.println(decimalFormat1.format(number));

    }

    @Test
    public void test10() {



    }


}
